package com.youedata.modular.controller;

import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youedata.base.pojo.page.LayuiPageFactory;
import com.youedata.base.tips.DCResponse;
import com.youedata.modular.entity.LawNoticeInfo;
import com.youedata.modular.entity.WaterCutOffNoticeInfo;
import com.youedata.modular.model.dto.LawNoticeInfoDTO;
import com.youedata.modular.service.ILawNoticeInfoService;
import com.youedata.sys.core.util.UserHolder;
import com.youedata.util.ToolUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 法律公告表控制器
 *
 * @author hst
 * @date 2021-07-12 18:22:29
 */
@Controller
@Api(value = "/lawNoticeInfo", tags = "法律公告表管理")
@RequestMapping("/lawNoticeInfo")
public class LawNoticeInfoController {

    private String PREFIX = "/lawNoticeInfo";

    @Autowired
    private ILawNoticeInfoService lawNoticeInfoService;

    /**
     * 新增
     *
     * @author hst
     * @date 2021-07-12
     */
    @ResponseBody
    @PostMapping("/addItem")
    @ApiOperation(value = "新增")
    public DCResponse<Void> addItem(LawNoticeInfoDTO lawNoticeInfoDto) {
        LawNoticeInfo lawNoticeInfo = new LawNoticeInfo();
        BeanUtils.copyProperties(lawNoticeInfoDto, lawNoticeInfo);
        lawNoticeInfo.setCreateUser(String.valueOf(UserHolder.getUserId()));
        lawNoticeInfo.setCreateTime(LocalDateTime.now());
        this.lawNoticeInfoService.save(lawNoticeInfo);
        return DCResponse.ok(null);
    }

    /**
     * 根据主键删除
     *
     * @author hst
     * @date 2021-07-12
     */
    @ResponseBody
    @PostMapping("/delete")
    @ApiOperation("删除")
    public DCResponse<Void> delete(@RequestParam("id") String id) {
        this.lawNoticeInfoService.removeById(id);
        return DCResponse.ok(null);
    }

    /**
     * 修改
     *
     * @author hst
     * @date 2021-07-12
     */
    @ResponseBody
    @PostMapping("/editItem")
    @ApiOperation("根据ID修改信息")
    public DCResponse<Void> editItem(LawNoticeInfoDTO lawNoticeInfoDto) {
        LawNoticeInfo lawNoticeInfo = new LawNoticeInfo();
        BeanUtils.copyProperties(lawNoticeInfoDto, lawNoticeInfo);
        lawNoticeInfo.setUpdateUser(UserHolder.getUserId());
        lawNoticeInfo.setUpdateTime(LocalDateTime.now());
        this.lawNoticeInfoService.updateById(lawNoticeInfo);
        return DCResponse.ok(null);
    }

    /**
     * 根据主键查询单条详情
     *
     * @author hst
     * @date 2021-07-12
     */
    @ResponseBody
    @PostMapping("/detail")
    @ApiOperation(value = "根据主键查询单条详情")
    public ResponseData detail(@RequestParam("id") String id) {
        return ResponseData.success(this.lawNoticeInfoService.getById(id));
    }

    /**
     * 分页查询列表
     *
     * @author hst
     * @date 2021-07-12
     */
    @ResponseBody
    @GetMapping("/list")
    @ApiOperation(value = "分页查询列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "page", value = "当前页码", required = true, defaultValue = "1", paramType = "query", dataType = "int", example = "1"),
        @ApiImplicitParam(name = "limit", value = "每页多少条数据", required = true, defaultValue = "10", paramType = "query", dataType = "int", example = "20")
    })
    public Object list(LawNoticeInfoDTO lawNoticeInfoDto) {
        LawNoticeInfo lawNoticeInfo = new LawNoticeInfo();
        lawNoticeInfoDto.setNoticeTitle(StringUtils.isEmpty(lawNoticeInfoDto.getNoticeTitle())?null:lawNoticeInfoDto.getNoticeTitle());

        BeanUtils.copyProperties(lawNoticeInfoDto, lawNoticeInfo);

        //获取分页参数
        Page page = LayuiPageFactory.defaultPage();
        String title = lawNoticeInfo.getNoticeTitle();
        //根据条件查询
        //page.setRecords(this.lawNoticeInfoService.page(page, Wrappers.query(lawNoticeInfo)).getRecords());

        //page.setRecords(this.lawNoticeInfoService.querytitlefuzzy(page,title));
        List<LawNoticeInfo> lawNoticeInfoList = this.lawNoticeInfoService.querytitlefuzzy(page,title);
        if (ObjectUtils.isNotEmpty(lawNoticeInfoList)) {
            lawNoticeInfoList.stream().map(x -> {
                if (ToolUtil.isNotEmpty(x.getCreateTime())) {
                    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd");
                    x.setCreateTimeStr(dtf.format(x.getCreateTime()));
                }
                return x;
            }).collect(Collectors.toList());
        }
        page.setRecords(lawNoticeInfoList);
        return LayuiPageFactory.createPageInfo(page);
    }

    /**
     * 跳转到主页面
     *
     * @author hst
     * @Date 2021-07-12
     */
    @RequestMapping()
    public String index() {
        return PREFIX + "/lawNoticeInfo.html";
    }

    /**
     * 新增页面
     *
     * @author hst
     * @Date 2021-07-12
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/lawNoticeInfo_add.html";
    }

    /**
     * 编辑页面
     *
     * @author hst
     * @Date 2021-07-12
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/lawNoticeInfo_edit.html";
    }

}
